# -*- coding: utf-8 -*-
"""
订单状态和支付相关的枚举定义
"""
from enum import Enum


class OrderStatus(Enum):
    """订单状态枚举"""
    # 基础状态
    PENDING = "pending"  # 待处理（购物车状态）
    SUBMITTED = "submitted"  # 已提交（等待支付）
    
    # 支付相关状态
    PAID = "paid"  # 已支付
    PAYMENT_FAILED = "payment_failed"  # 支付失败
    
    # 履约状态
    SHIPPED = "shipped"  # 已发货
    DELIVERED = "delivered"  # 已送到
    SIGNED = "signed"  # 已签收
    
    # 线下支付特有状态
    RECEIPT_PENDING = "receipt_pending"  # 等待回执
    RECEIPT_UPLOADED = "receipt_uploaded"  # 回执已上传
    
    # 完成状态
    COMPLETED = "completed"  # 已完成
    CANCELLED = "cancelled"  # 已取消
    REFUNDED = "refunded"  # 已退款


class PaymentType(Enum):
    """支付类型枚举"""
    ONLINE = "online"  # 线上支付
    OFFLINE = "offline"  # 线下支付


class PaymentMethod(Enum):
    """支付方式枚举"""
    WECHAT = "wechat"  # 微信支付
    ALIPAY = "alipay"  # 支付宝
    OFFLINE_TRANSFER = "offline_transfer"  # 线下转账
    OFFLINE_CASH = "offline_cash"  # 现金支付


class PaymentStatus(Enum):
    """支付状态枚举"""
    PENDING = "pending"  # 待支付
    PROCESSING = "processing"  # 支付处理中
    PAID = "paid"  # 已支付
    FAILED = "failed"  # 支付失败
    CANCELLED = "cancelled"  # 支付取消
    REFUNDED = "refunded"  # 已退款


class OrderStatusTransition:
    """订单状态流转定义"""
    
    # 线上支付流程
    ONLINE_PAYMENT_FLOW = [
        OrderStatus.PENDING,      # 未下单
        OrderStatus.SUBMITTED,    # 已下单
        OrderStatus.PAID,         # 已支付
        OrderStatus.SHIPPED,      # 已发货
        OrderStatus.DELIVERED,    # 已送到
        OrderStatus.SIGNED,       # 已签收
        OrderStatus.COMPLETED     # 完成
    ]
    
    # 线下支付流程
    OFFLINE_PAYMENT_FLOW = [
        OrderStatus.PENDING,         # 未下单
        OrderStatus.SUBMITTED,       # 已下单
        OrderStatus.SHIPPED,         # 已发货
        OrderStatus.DELIVERED,       # 已送到
        OrderStatus.SIGNED,          # 已签收
        OrderStatus.RECEIPT_PENDING, # 收款回执
        OrderStatus.COMPLETED        # 完成
    ]
    
    # 状态转换规则
    VALID_TRANSITIONS = {
        OrderStatus.PENDING: [OrderStatus.SUBMITTED, OrderStatus.CANCELLED],
        OrderStatus.SUBMITTED: [
            OrderStatus.PAID,           # 线上支付成功
            OrderStatus.SHIPPED,        # 线下支付直接发货
            OrderStatus.PAYMENT_FAILED, 
            OrderStatus.CANCELLED
        ],
        OrderStatus.PAID: [OrderStatus.SHIPPED, OrderStatus.REFUNDED],
        OrderStatus.SHIPPED: [OrderStatus.DELIVERED],
        OrderStatus.DELIVERED: [OrderStatus.SIGNED],
        OrderStatus.SIGNED: [
            OrderStatus.COMPLETED,        # 线上支付完成
            OrderStatus.RECEIPT_PENDING   # 线下支付等待回执
        ],
        OrderStatus.RECEIPT_PENDING: [OrderStatus.RECEIPT_UPLOADED],
        OrderStatus.RECEIPT_UPLOADED: [OrderStatus.COMPLETED],
        OrderStatus.PAYMENT_FAILED: [OrderStatus.SUBMITTED, OrderStatus.CANCELLED],
        # 终态
        OrderStatus.COMPLETED: [],
        OrderStatus.CANCELLED: [],
        OrderStatus.REFUNDED: []
    }
    
    @classmethod
    def can_transition(cls, from_status: OrderStatus, to_status: OrderStatus) -> bool:
        """检查状态转换是否合法"""
        return to_status in cls.VALID_TRANSITIONS.get(from_status, [])
    
    @classmethod
    def get_next_status(cls, current_status: OrderStatus, payment_type: PaymentType) -> OrderStatus:
        """根据当前状态和支付类型获取下一个状态"""
        if payment_type == PaymentType.ONLINE:
            if current_status == OrderStatus.SUBMITTED:
                return OrderStatus.PAID
            elif current_status == OrderStatus.SIGNED:
                return OrderStatus.COMPLETED
        elif payment_type == PaymentType.OFFLINE:
            if current_status == OrderStatus.SUBMITTED:
                return OrderStatus.SHIPPED
            elif current_status == OrderStatus.SIGNED:
                return OrderStatus.RECEIPT_PENDING
        
        # 通用状态转换
        transitions = {
            OrderStatus.PAID: OrderStatus.SHIPPED,
            OrderStatus.SHIPPED: OrderStatus.DELIVERED,
            OrderStatus.DELIVERED: OrderStatus.SIGNED,
            OrderStatus.RECEIPT_PENDING: OrderStatus.RECEIPT_UPLOADED,
            OrderStatus.RECEIPT_UPLOADED: OrderStatus.COMPLETED
        }
        
        return transitions.get(current_status)


class OrderStatusDisplay:
    """订单状态显示信息"""
    
    STATUS_INFO = {
        OrderStatus.PENDING: {
            "name": "未下单",
            "description": "商品在购物车中",
            "color": "#999999",
            "icon": "cart"
        },
        OrderStatus.SUBMITTED: {
            "name": "已下单",
            "description": "订单已提交，等待支付",
            "color": "#ff9500",
            "icon": "clock"
        },
        OrderStatus.PAID: {
            "name": "已支付",
            "description": "支付成功，等待发货",
            "color": "#007aff",
            "icon": "checkmark"
        },
        OrderStatus.PAYMENT_FAILED: {
            "name": "支付失败",
            "description": "支付未成功，请重新支付",
            "color": "#ff3b30",
            "icon": "exclamation"
        },
        OrderStatus.SHIPPED: {
            "name": "已发货",
            "description": "商品已发出，正在运输中",
            "color": "#34c759",
            "icon": "truck"
        },
        OrderStatus.DELIVERED: {
            "name": "已送到",
            "description": "商品已送达，请尽快签收",
            "color": "#30d158",
            "icon": "location"
        },
        OrderStatus.SIGNED: {
            "name": "已签收",
            "description": "商品已签收确认",
            "color": "#32d74b",
            "icon": "hand"
        },
        OrderStatus.RECEIPT_PENDING: {
            "name": "等待回执",
            "description": "请上传付款回执",
            "color": "#ff9500",
            "icon": "document"
        },
        OrderStatus.RECEIPT_UPLOADED: {
            "name": "回执已上传",
            "description": "付款回执已上传，等待确认",
            "color": "#007aff",
            "icon": "checkmark-circle"
        },
        OrderStatus.COMPLETED: {
            "name": "已完成",
            "description": "订单已完成",
            "color": "#28a745",
            "icon": "checkmark-done"
        },
        OrderStatus.CANCELLED: {
            "name": "已取消",
            "description": "订单已取消",
            "color": "#6c757d",
            "icon": "close"
        },
        OrderStatus.REFUNDED: {
            "name": "已退款",
            "description": "订单已退款",
            "color": "#dc3545",
            "icon": "arrow-undo"
        }
    }
    
    @classmethod
    def get_status_info(cls, status: OrderStatus) -> dict:
        """获取状态显示信息"""
        return cls.STATUS_INFO.get(status, {
            "name": "未知状态",
            "description": "",
            "color": "#999999",
            "icon": "help"
        }) 